package com.cm.sys.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.aop.NotCheckLogin;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.utils.JwtUtils;
import com.cm.sys.system.entity.BusinessInfo;
import com.cm.sys.system.entity.Menu;
import com.cm.sys.system.entity.User;
import com.cm.sys.system.service.BusinessService;
import com.cm.sys.system.service.IndexService;
import com.cm.sys.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Creator:郭兆鹏
 * Date:2020/1/14
 * 作用:
 */
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;
    @Autowired
    private MenuService menuService;

    //根据当前登录的用户查询所在项目组的信息
    @RequestMapping(value = "/getCount",method = RequestMethod.POST)
    public ResponseResult getCount(RequestParameter rp){
        JSONObject args = rp.getparameters();
        String token = rp.getToken();
        User user = JwtUtils.validateToken(token);
        Map<String,Object> map = new HashMap<>();
        if(user.getUserRole()==1){
            //此用户为超级管理员用户，查询所有的业务线，用户组，以及用户个数
            map = getAllCount();
        }else{
            map = getCountByUser(user.getId());
        }

        ResponseResult result = new ResponseResult();
        result.setCode(ApiResponseCode.SUCCESS.getCode());
        result.setMap(map);
        result.setMessage(ApiResponseCode.SUCCESS.getMessage());
        return result;
    }


    private Map<String, Object> getCountByUser(Integer id) {
        Map<String, Object> map = new HashMap<String,Object>();
        Integer roleCount = indexService.getRoleByUser(id);
        Integer userCount = indexService.getWorkMateCount(id);
        List<BusinessInfo> businessInfos = indexService.getBusinessByUser(id);
        map.put("roleCount",roleCount);
        map.put("userCount",userCount);
        map.put("businessCount",businessInfos.size());
        List<Map<String, Object>> businessList = getMaps(businessInfos);
        map.put("businessInfo",businessList);
        return map;
    }

    private Map<String, Object> getAllCount() {
        Map<String, Object> map = new HashMap<String,Object>();
        Integer roleCount = indexService.getRoleCount();
        Integer userCount = indexService.getUserCount();
        List<BusinessInfo> businessInfos = indexService.getBusinessCount();
        map.put("roleCount",roleCount);
        map.put("userCount",userCount);
        map.put("businessCount",businessInfos.size());
        List<Map<String, Object>> businessList = getMaps(businessInfos);
        map.put("businessInfo",businessList);
        return map;
    }

    private List<Map<String, Object>> getMaps(List<BusinessInfo> businessInfos) {
        List<Map<String,Object>> businessList = new ArrayList<>();
        List<Menu> menus = new ArrayList<>();

        List<Integer> menuIds = new ArrayList<>();
        if(businessInfos!=null&&businessInfos.size()>0){
            for (BusinessInfo businessInfo : businessInfos) {
                menuIds.add(businessInfo.getMenuId());
            }
            menus = menuService.selectBusinessMenu(menuIds);
            for (Menu menu : menus) {
                HashMap<String, Object> info = new HashMap<>();
                info.put("businessId",menu.getId());
                info.put("businessName",menu.getParentName());
                info.put("createTime",menu.getCreateTime());
                businessList.add(info);
            }
        }
        return businessList;
    }
}
